<?php
/**
 * 描述：自动加载类，用于辅助写代码
 * 作者：Zoro.Zhu
 */

//打印封装
function p($array){
    echo '<pre>';
    print_r($array);
}

function pe($array){
    echo '<pre>';
    print_r($array);
    exit;
}

/**
 * 描述 : 去除参数前后的空格
 * 参数 :
 *      param 需要去除空格的参数 string,array
 * 作者 : Adam Zhao
 */
function removeBlank($param)
{
    if (is_string($param)) {
        $result = trim($param);
    } elseif (is_array($param)) {
        $result = [];
        foreach ($param as $key => $value) {
            $result[trim($key)] = removeBlank($value);
        }
    } else {
        $result = $param;
    }
    return $result;
}

/**
 * 描述 : 通过sql语句查询ES集群
 * 作者 : Zoro.Zhu
 */
function esSql($sql, $from = 10, $size = 10) {
    $node = [
        '193.112.113.247:9200',//节点
    ];
    $client = Elasticsearch\ClientBuilder::create()->setHosts($node)->build();
    $data = $client->sql()->translate(['body' => ['query' => $sql]]);
    $query['size'] = $size;
    $query['from'] = $from;
    !empty($data['_source']['includes']) && $query['_source_includes'] = $data['_source']['includes'];
    !empty($data['query']) && $query['body']['query'] = $data['query'];
    !empty($data['sort']) && $query['body']['sort'] = $data['sort'];
    !empty($data['stored_fields']) && $query['body']['stored_fields'] = $data['stored_fields'];
    $query['index'] = 'dxm_order';
    !empty($data['aggregations']) && $query['body']['aggregations'] = $data['aggregations'];
    $res = $client->search($query);
    $return = [];
    if(!empty($res['aggregations'])){
        foreach ($data['aggregations']['groupby']['aggregations'] as $id => $a){
            $key = !empty($a['sum']['field']) ? $a['sum']['field'] : 'c'; //$a['filter']['exists']['field']
            $value = $res['aggregations']['groupby']['buckets'][0][$id];
            $return[0][$key] = isset($value['value']) ? $value['value'] : $value['doc_count'];
        }
    } else if(!empty($res['hits'])){
        foreach($res['hits']['hits'] as $row){
            $return[] = $row['_source'];
        }
//        $return['total'] = $res['hits']['total'];
    }
    return $return;
}

/**
 * 描述 : 针对可能存在浮点型的字段数字扩大一百倍
 * 参数 :
 *      param {
 *          "field" => "value"
 *      }
 *      type : 1:扩大一百倍，2-缩小一百倍
 * 作者 : Adam Zhao
 */
function floatExpandHundred(&$param, $type = 1)
{
    $numberField = of::config('env.numberField');
    if (!empty($param)) {
        if (is_array($param)) {
            //数组
            foreach ($param as $field => &$value) {
                is_array($value) && floatExpandHundred($value, $type);
                if (is_string($value) && isJsonString($value)) {
                    $value = json_decode($value, true);
                    floatExpandHundred($value, $type);
                }
                if (in_array($field, $numberField) && is_numeric($value)) {
                    if ($type == 1) {
                        //兼容两位以上的小数，四舍五入
                        $value = round($value, 2);
                        //扩大100倍
                        $value = bcmul($value, 100);
                    } elseif ($type == 2) {
                        //还原
                        $value = bcdiv($value, 100, 2);
                        $valArr = explode('.', strval($value));
                        if (isset($valArr[1])) {
                            if ($valArr[1] == '00') {
                                $value = intval($value);
                            } else {
                                $value = floatval($value);
                            }
                        }
                    }
                }
            }
        } elseif (is_numeric($param)) {
            //数字
            if ($type == 1) {
                //兼容两位以上的小数，四舍五入
                $param = round($param, 2);
                //扩大100倍
                $param = bcmul($param, 100);
            } elseif ($type == 2) {
                //还原
                $param = bcdiv($param, 100, 2);
            }
        }
    }
    return $param;
}